package com.dc.free.util;

import javax.servlet.http.HttpServletRequest;

import com.jfinal.kit.StrKit;

/**
 * 
 * @author linquan
 *
 */
public class WebUtil {
	
	/**
	 * 获取请求地址
	 * @param request
	 * @return
	 */
	public static String getPath(HttpServletRequest request){
		
		String base = request.getScheme() 
					+ "://" 
					+ request.getServerName() 
					+ (request.getServerPort() == 80 ? "" : ":" + request.getServerPort()) 
					+ request.getContextPath();
		
		//去除结尾'/'
		return base.endsWith("/") ? base.substring(0, base.length()-1) : base;
	}
	
	/**
	 * 获取请求地址(不带前缀)
	 * @param request
	 * @return
	 */
	public static String getPathWithoutProfix(HttpServletRequest request){
		
		String base =  request.getContextPath();		
		
		//去除结尾'/'
		return base.endsWith("/") ? base.substring(0, base.length()-1) : base;
	}
	
	/**
	 * 获取完整的请求及参数
	 * @param request
	 * @return
	 */
	public static String getUrlWithParameter(HttpServletRequest request) {
		String queryString = request.getQueryString();
		if(StrKit.isBlank(queryString)) 
			return request.getRequestURL().toString();
		return request.getRequestURL() + "?" + request.getQueryString();
	}
	
}
